package com.cw.starter.compileflow.helper;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.compileflow.engine.ProcessEngine;
import com.alibaba.compileflow.engine.ProcessEngineFactory;

import lombok.extern.slf4j.Slf4j;

/**
 * Bpm流程编排工具类
 *
 * @author chenw
 * @date 2021/8/3
 */
@Slf4j
public class BpmHelper {
    /**
     * 加载 bpm文件
     * @param bpmCodes
     * @return
     */
    public static Boolean load(String bpmCodes){
       return loadFile(bpmCodes,false);
    }
    /**
     * 重载 bpm文件
     * @param bpmCodes
     * @return
     */
    public static Boolean reload(String bpmCodes){
        return loadFile(bpmCodes,true);
    }

    public static Boolean loadFile(String bpmCodes,Boolean reload){
        if(StringUtils.isEmpty(bpmCodes)){
            log.error("bpm codes is empty,please check!");
            return false;
        }
        ProcessEngine processEngine = ProcessEngineFactory.getProcessEngine();
        String [] bpmCodeList = bpmCodes.split(",");
        for(String bpmCode : bpmCodeList){
            if(reload){
                processEngine.reload(bpmCode);
            }else{
                processEngine.load(bpmCode);
            }
        }
        return true;
    }
}
